<template>
  <div class="fans-container">
    <el-card>
      <div slot="header">
        <!-- 面包屑路径导航 -->
        <my-breadcrumb>粉丝管理</my-breadcrumb>
        <!-- /面包屑路径导航 -->
      </div>
      <!-- tabs组件 -->
      <el-tabs v-model="activeName" type="card" @tab-click="hTabClick">
        <el-tab-pane label="粉丝列表" name="list">
          <!-- 列表 -->
          <div class="fans_list">
            <div class="fans_item"
            v-for="(fan, idx) in list"
            :key="idx">
              <el-avatar :size="80" :src="fan.photo"></el-avatar>
              <p>{{fan.name}}</p>
              <el-button type="primary" plain size="small">+关注</el-button>
            </div>
          </div>
          <!-- 分页 -->
          <el-pagination
            background
            layout="prev, pager, next"
            :page-size="per_page"
            @current-change="hChangePager(page)"
            :total="total">
          </el-pagination>
        </el-tab-pane>
        <el-tab-pane label="粉丝画像" name="img">
          <!-- 准备dom容器，让echarts在这里画图
          注意要指定宽高；设置ref，方便访问这个dom-->
          <div ref="main" style="width: 600px;height:400px;"></div>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
// 使用echarts的步骤
// 1.准备dom容器
// 2.引入echarts
// 3.画图
import echarts from 'echarts';
import { getFans, getFansStatistics } from '@/api/fans.js';
import MyBreadcrumb from '../../components/MyBreadcrumb.vue';
export default {
  name: 'my-fans',
  data () {
    return {
      // tabs的当前激活选项卡的name属性值
      activeName: 'list',
      per_page: 20,
      page: 1,
      list: [], // 粉丝列表
      total: 0,
      // 测试头像
      circleUrl: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
    };
  },
  components: {
    MyBreadcrumb
  },
  computed: { },
  created () {
    this.loadFans();
  },
  mounted () { },
  methods: {
    async loadFans () {
      try {
        const res = await getFans(this.page, this.per_page);
        // 保存到数据项
        this.total = res.data.data.total_count;
        this.list = res.data.data.results;
      } catch (error) {
        this.$message.error(error.message);
      }
    },
    // 翻页
    hChangePager (curPage) {
      this.page = curPage;
      this.loadFans();
    },
    // 点击切换tab
    hTabClick (tab) {
      if (tab.name === 'img') {
        this.draw();
      }
    },
    async draw () {
      const result = await getFansStatistics();
      const age = result.data.data.age;
      // 得到的数据是一个对象,对后端数据进行加工
      // le小于；gt大于
      const ageY = [];
      const ageX = [];
      for (const key in age) {
        ageY.push(age[key]);
        ageX.push(key.replace('le', '小于').replace('gt', '大于') + '岁');
      }

      // echarts画图
      // 1.初始化echarts对象
      const echartsobj = echarts.init(this.$refs.main);
      // 2.设置配置项
      const option = {
        xAxis: {
          type: 'category',
          data: ageX
        },
        yAxis: {
          type: 'value'
        },
        series: [{
          data: ageY,
          type: 'bar',
          showBackground: true,
          backgroundStyle: {
            color: 'rgba(220, 220, 220, 0.8)'
          }
        }],
        color: 'pink'
      };
      // 3.使用配置项和数据显示图表
      echartsobj.setOption(option);
    }
  }
};
</script>

<style scoped lang='less'>
.fans_list {
  .fans_item {
    width: 120px;
    height: 170px;
    border: 1px dashed #ddd;
    text-align: center;
    padding-top: 10px;
    display: inline-block;
    margin-right: 30px;
    margin-bottom: 30px;
    p {
      margin: 10px 0;
    }
  }
}
</style>
